package com.dmagic.cms.common.query;

import java.io.Serializable;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.dmagic.cms.common.util.StringUtil;

/**
 * <h3>Class name</h3> <h4>Description</h4> <h4>Special Notes</h4>
 * 
 * @version 0.2
 * @author songlin.li 2012-4-26
 */

public class ConditionsBean implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -6566499451064404648L;

    /**
     * equals
     */
    public final static String CONDITION_EQ = "eq";

    /**
     * left like
     */
    public final static String CONDITION_LL = "ll";

    /**
     * right like
     */
    public final static String CONDITION_RL = "rl";

    /**
     * full like
     */
    public final static String CONDITION_FL = "fl";

    /**
     * between
     */
    public final static String CONDITION_BW = "bw";

    public final static String EQUAL = " = ";

    public final static String LIKE = " LIKE ";

    public final static String AND = " AND ";

    public final static String OR = " OR ";

    private boolean isIgnoreCase = false;

    private String property;

    private Object value;

    private Object anotherValue;

    private String groupType = AND;

    /**
     * default
     */
    private String condition = CONDITION_EQ;

    /**
     * @return the property
     */
    public String getProperty() {
        return property;
    }

    /**
     * @param property the property to set
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * @return the value
     */
    public Object getValue() {
        if (value instanceof String) {
            if (StringUtil.isBlank((String) value)) {
                return null;
            } else if (value.toString().equals("%null%")) {
                return null;
            } else if (StringUtil.equals(condition, CONDITION_LL)) {
                return "%" + value;
            } else if (StringUtil.equals(condition, CONDITION_RL)) {
                return value + "%";
            } else if (StringUtil.equals(condition, CONDITION_FL)) {
                return "%" + value + "%";
            }
        }
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(Object value) {
        this.value = value;
    }

    public Object getAnotherValue() {
        return anotherValue;
    }

    public void setAnotherValue(Object anotherValue) {
        this.anotherValue = anotherValue;
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    /**
     * @return
     */
    public String getGroupType() {
        return groupType;
    }

    /**
     * @param String
     */
    public void setGroupType(String groupType) {
        groupType = StringUtil.startsWith(groupType, " ") ? groupType : " " + groupType;
        groupType = StringUtil.endsWith(groupType, " ") ? groupType : groupType + " ";
        this.groupType = groupType;
    }

    /**
     * @return
     */
    public boolean isIgnoreCase() {
        return isIgnoreCase;
    }

    /**
     * @param boolean
     */
    public void setIgnoreCase(boolean isIgnoreCase) {
        this.isIgnoreCase = isIgnoreCase;
    }

}
